package game.module.itemnew.dao;

import game.module.itemnew.beans.ItemTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

@Repository
public class ItemTemplateDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<ItemTemplate> getAllTemplate() {
		return jdbcTemplate.query("select * from item_template", new ResultSetExtractor<List<ItemTemplate>>() {
			@Override
			public List<ItemTemplate> extractData(ResultSet rs) throws SQLException, DataAccessException {
				List<ItemTemplate> itemTemplateList = new ArrayList<ItemTemplate>();
				while (rs.next()) {
					ItemTemplate template = new ItemTemplate();
					template.setAttack(rs.getInt("attack"));
					template.setBigIco(rs.getInt("bigIco"));
					template.setCrit(rs.getInt("crit"));
					template.setDefence(rs.getInt("defence"));
					template.setDesc(rs.getString("desc"));
					template.setDodge(rs.getInt("dodge"));
					template.setDurability(rs.getInt("durability"));
					template.setGrade(rs.getByte("grade"));
					template.setHit(rs.getInt("hit"));
					template.setHp(rs.getInt("hp"));
					template.setIco(rs.getInt("ico"));
					template.setId(rs.getInt("id"));
					template.setLimitGrade(rs.getInt("limitGrade"));
					template.setMp(rs.getInt("mp"));
					template.setName(rs.getString("name"));
					template.setPosition(rs.getByte("position"));
					template.setRepairMoney(rs.getInt("repairMoney"));
					template.setRepeat(rs.getShort("repeat"));
					template.setSp(rs.getInt("sp"));
					itemTemplateList.add(template);
				}
				return itemTemplateList;
			}
		});
	}

}
